//
//  Material_CollectionViewCell.swift
//  MyFood
//
//  Created by dllo on 16/2/16.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit
import SwiftyJSON

class Material_CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    var collectionView : UICollectionView!

    var materialBigArr : Array<F_Material_Collection_Model>!
    var materialSmaArr : Array<Array<F_Material_Second_Collection_Model>>!
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        createCollection()
        createData()
        
    }

    func createCollection(){

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSizeMake(BASE_WIDTH, 200)
        self.collectionView = UICollectionView(frame: CGRectMake(0, 0, BASE_WIDTH, BASE_HEIGHT), collectionViewLayout: flowLayout)
        self.contentView.addSubview(self.collectionView)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 15
        collectionView.pagingEnabled = true
        collectionView.bounces = false
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.lightGrayColor()
        
        

        
        
        
    self.collectionView.registerNib(UINib(nibName: "M_Matrial_CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        
    self.collectionView.registerNib(UINib(nibName: "Matrial_CollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "UICollectionElementKindSectionHeader", withReuseIdentifier: "reusable")

    }

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
        var v = Matrial_CollectionReusableView()
        
        if kind == UICollectionElementKindSectionHeader{

            v = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "reusable", forIndexPath: indexPath) as! Matrial_CollectionReusableView
            if self.materialBigArr != nil{
            let model:F_Material_Collection_Model = self.materialBigArr[indexPath.section]

            v.sectionTitle.text = model.text
            }
        }
        return v
    }

    //返回HeadView的宽高
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{

        return CGSize(width: 375, height: 40)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        if self.materialBigArr != nil{
            return self.materialBigArr.count
        }else{
            return 0
        }
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as!M_Matrial_CollectionViewCell
        
        cell.materialSmaArr = self.materialSmaArr[indexPath.section]
        
        return cell
    }


    func createData(){
        
        XRAFNetworkTool.POSTUrl(BASE_URL, body: "methodName=MaterialSubtype&version=4.3.1&", requestBodyStyle: XRRequestStyle.String, headFile: nil, response: XRResponseStyle.XRJSON, success: { (dtaTask, request) -> Void in
            
            let json = JSON(request)
            let arr = json["data"]["data"].array
            
            self.materialBigArr = Array()
            self.materialSmaArr = Array()
            
            for temp in arr!{

                let dic : Dictionary = temp.dictionaryObject!
                let model = F_Material_Collection_Model(dic:dic)
                self.materialBigArr.append(model)
                
                let smallarr = temp["data"].array
                var temparr = [F_Material_Second_Collection_Model]()

                for temp1 in smallarr!{
                    
                    let dic : Dictionary = temp1.dictionaryObject!
                    let model1 = F_Material_Second_Collection_Model(dic:dic)
                    temparr.append(model1)
                    
                }
                self.materialSmaArr.append(temparr)
                
            }

            self.collectionView.reloadData()
            
            //print(self.materialSmaArr.count)
            
            }) { (dtaTask, request) -> Void in
                
        }
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}
